<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="130px">
          <el-form-item label="区域名称" prop="areaName">
            <el-input v-model="form.areaName" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="区域类型" prop="areaType">
            <!--<el-input v-model="form.nodeType" style="width: 370px;"/>-->
            <el-select v-model="form.areaType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.area_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <!--<el-select v-model.number="form.areaMonitor.id" placeholder="请选择" style="width: 370px">-->
          <!--<el-option v-for="item in areaMonitors" :key="item.id" :label="item.name" :value="item.id" />-->
          <!--</el-select>-->

          <!--<el-form-item label="区域监控" prop="areaMonitors">-->
          <!--&lt;!&ndash;<el-input v-model="form.nodeType" style="width: 370px;"/>&ndash;&gt;-->
          <!--<el-select v-model="form.areaMonitor" filterable placeholder="请选择">-->
          <!--<el-option-->
          <!--v-for="item in dict.area_monitor"-->
          <!--:key="item.id"-->
          <!--:label="item.label"-->
          <!--:value="item.value"-->
          <!--/>-->
          <!--</el-select>-->

          <el-form-item label="区域监控" prop="areaMonitorList">
            <el-select v-model="form.areaMonitorList" placeholder="请选择" style="width: 370px" multiple @change="changeAreaMonitors">
              <el-option v-for="item in areaMonitors" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <!--<el-form-item label="区域监控" prop="areaMonitor">-->
          <!--<el-select v-model="form.areaMonitor" multiple placeholder="请选择" style="width: 370px">-->
          <!--<el-option v-for="item in areaMonitors" :key="item.id" :label="item.name" :value="item.id" />-->
          <!--</el-select>-->
          <!--</el-form-item>-->

          <!--<el-input v-model="form.areaMonitor" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <!--<el-form-item label="是否参加人数统计">-->
          <!--<el-input v-model="form.joinUserCount" style="width: 370px;" />-->
          <!--</el-form-item>-->

          <el-form-item label="是否参加人数统计">
            <el-radio-group v-model="form.joinUserCount" style="width: 178px">
              <el-radio label="是">是</el-radio>
              <el-radio label="否">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="关联围栏区域">
            <el-input v-model="form.relFenceArea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="areaName" label="区域名称" />
        <!--<el-table-column prop="areaType" label="区域类型" />-->
        <el-table-column prop="areaType" label="区域类型">
          <template slot-scope="scope">
            {{ dict.label.area_type[scope.row.areaType] }}
          </template>
        </el-table-column>

        <el-table-column prop="areaMonitorName" label="区域监控" />

        <!--<el-table-column prop="areaMonitor" label="区域监控" >-->
        <!--<template slot-scope="scope">-->
        <!--{{ dict.label.area_monitor[scope.row.areaMonitor] }}-->
        <!--</template>-->
        <!--</el-table-column>-->

        <el-table-column prop="joinUserCount" label="是否参加人数统计" />
        <el-table-column prop="relFenceArea" label="关联围栏区域" />
        <el-table-column v-permission="['admin','areaManage:edit','areaManage:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudAreaMonitors from '@/api/system/cameraMonitor'
import crudAreaManage from '@/api/system/areaManage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, createTime: null, updateTimee: null, createBy: null, updateBy: null, areaName: null, areaType: null, areaMonitor: null, joinUserCount: null, relFenceArea: null }
export default {
  name: 'AreaManage',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['area_type', 'area_monitor'],

  cruds() {
    return CRUD({ title: '区域管理', url: 'api/areaManage', sort: 'id,desc', crudMethod: { ...crudAreaManage }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'areaManage:add'],
        edit: ['admin', 'areaManage:edit'],
        del: ['admin', 'areaManage:del']
      },
      areaMonitors: [], areaMonitor: '', areaMonitorName: '',
      rules: {
        areaName: [
          { required: true, message: '区域名称不能为空', trigger: 'blur' }
        ],
        areaType: [
          { required: true, message: '区域类型不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'areaName', display_name: '区域名称' }
      ]
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      this.initSelect()

      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    },
    initSelect() {
      crudAreaMonitors.getCameraMonitor().then(res => {
        console.log(res)
        this.areaMonitors = res.content
      })
    },
    changeAreaMonitors(item) {
      this.areaMonitor = item.id
      console.log(item)
    }
  }
}
</script>

<style scoped>

</style>
